package cn.com.generaldata.os.ws.impl.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.jasig.cas.client.util.CommonUtils;
import org.nutz.ioc.annotation.InjectName;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.com.generaldata.os.util.CacheManager;
import cn.com.generaldata.os.vo.ReportVO;
import cn.com.generaldata.os.vo.ResultVO;
import cn.com.generaldata.os.vo.UserVO;
import edu.emory.mathcs.backport.java.util.Arrays;

@InjectName
@Component
public class ReportDelAction {

	@Autowired
	private CacheManager cacheManager;

	@Autowired
	private ReportAction reportAction;

	/**
	 * 获取清除报表列表
	 * */
	@At("/dataManage/delReportData")
	@Ok("jsp:/static/ocean/dataManage/reportQueryDelete")
	public ResultVO<ReportVO> reportDeleteList(@Param("reportProxyId") String reportProxyId,
			@Param("userUnit") String userUnit, @Param("typeId") String reportSystem,
			@Param("reportTypeId") String reportTypeCode, @Param("versionId") String reportVersion,
			@Param("searchDate") Date startingReportDate, @Param("endDate") Date endingReportDate,
			@Param("p") Integer pageNum, @Param("reportNo") String typeNo, @Param("industryId") String industryId,
			HttpServletRequest reqeust) {

		UserVO user = (UserVO) reqeust.getSession().getAttribute("user");

		String userId = user.getId();
		String userAreaId = user.getUserAreaId();
		reportProxyId = userId;

		//查询所有报表制度
		reqeust.setAttribute("sysCodeList", reportAction.queryType());

		if (reportSystem == null) {
			return null;
		}

		//获取频率类型信息
		reqeust.setAttribute("reportTimetype", cacheManager.getReportTimetype());
		if (pageNum == null) {
			pageNum = new Integer(1);
		}
		if (CommonUtils.isNotBlank(typeNo)) {
			//			try {
			//				typeNo = new String(typeNo.getBytes("ISO8859-1"), "utf-8");				
			//			} catch (UnsupportedEncodingException e) {
			//				// TODO Auto-generated catch block
			//				e.printStackTrace();
			//			}
			reqeust.setAttribute("typeNo", typeNo);
		}
		if (CommonUtils.isNotBlank(userUnit)) {
			//			try {
			//				userUnit = new String(userUnit.getBytes("ISO8859-1"), "utf-8");
			//			} catch (UnsupportedEncodingException e) {
			//				// TODO Auto-generated catch block
			//				e.printStackTrace();
			//			}
			reqeust.setAttribute("userUnit", userUnit);
		}

		return reportAction.queryReportsDelete(userAreaId, userUnit, reportSystem, reportTypeCode, industryId,
				reportVersion, startingReportDate, endingReportDate, pageNum, typeNo);
	}

	/**
	 * 获取清除报表列表
	 * */
	@At("/dataManage/delReport")
	@Ok("json")
	public void deleteReports(@Param("delIds") String delIds) {

		if (CommonUtils.isNotBlank(delIds)) {
			String[] param = delIds.split(",");
			List<String> ids = Arrays.asList(param);
			reportAction.deleteReports(ids);
		}
	}

}
